package mipo.top.pet.Service.impl;

import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.base.util.BaiduAuditUtils;
import mipo.top.pet.Service.IPetDetailService;
import mipo.top.pet.Service.IPetService;
import mipo.top.pet.Service.IPetonlineAuditLogService;
import mipo.top.pet.domain.Pet;
import mipo.top.pet.domain.PetDetail;
import mipo.top.pet.domain.PetonlineAuditLog;
import mipo.top.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PetServiceimpl extends BaseServiceimpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IPetonlineAuditLogService petonlineAuditLogService;
    @Autowired
    private IPetDetailService petDetailService;
    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        StringBuffer buffer = new StringBuffer();
        Map<Long, Pet> collect = petMapper.findall().stream().filter(pet -> pet.getState()==0).collect(Collectors.toMap(Pet::getId, g -> g));
        ids.forEach(id->{
            Pet pet = collect.get(id);
            if (pet!=null){
                if(!BaiduAuditUtils.TextCensor(pet.getName())){
                    buffer.append("百度AI文本校验未通过，宠物名违规");
                }
                String[] strings = pet.getResources().split(",");
                for (String string : strings) {
                    if (!BaiduAuditUtils.ImgCensor(string)){
                        buffer.append("百度AI图片校验未通过，宠物图片违规");
                    }
                }
                if(buffer.length()>0) {
                    PetonlineAuditLog petonlineAuditLog = new PetonlineAuditLog();
                    petonlineAuditLog.setAuditTime(date);
                    petonlineAuditLog.setPetId(pet.getId());
                    petonlineAuditLog.setNote(buffer.toString());
                    petonlineAuditLog.setState(0);
                    petonlineAuditLogService.save(petonlineAuditLog);
                    return;
                }
                pet.setState(1);
                pet.setOnsaletime(date);
                petMapper.update(pet);
            }

        });

    }

    @Override
    public Pet getById(Long id) {
        return petMapper.getById(id);
    }

    @Override
    public void save(Pet pet) {
        pet.setCreatetime(new Date());
        super.save(pet);
        PetDetail petDetail = null;
        if(pet.getDetail()==null){
            petDetail=new PetDetail();
        }else {
            petDetail = pet.getDetail();
        }
        petDetail.setPetId(pet.getId());
        petDetailService.save(petDetail);
    }

    @Override
    public void update(Pet pet) {
        if(pet.getResources()==""){
            pet.setResources(null);
        }
        super.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailService.update(detail);
    }
}
